; Crappy SNDH-player
;
; loader.s
;
; June 9, 2003
;
; Anders Eriksson
; ae@dhs.nu

		section	text

	ifeq	ttpfile
		move.l	#testname,filename
	endc

		move.w	#0,-(sp)				;fsfirst() get fileinfo
		move.l	filename,-(sp)				;
		move.w	#$4e,-(sp)				;
		trap	#1					;
		addq.l	#8,sp					;

		tst.l	d0					;file found ?
		beq.s	.size					;yep

		move.l	#text_file,error_text			;no
		bra.w	exit					;exit

.size:		move.l	dta+26,filelength			;get file-legth
		move.l	filelength,reservelength		;

		;load 4 bytes and check for ICE!
		move.w	#0,-(sp)				;fopen()
		move.l	filename,-(sp)				;
		move.w	#$3d,-(sp)				;
		trap	#1					;
		addq.l	#8,sp					;
		move.w	d0,filenumber				;filenumber
		pea	icecheck				;buffer
		move.l	#4,-(sp)				;length
		move.w	filenumber,-(sp)			;filenumber
		move.w	#$3f,-(sp)				;
		trap	#1					;
		lea	12(sp),sp				;
		move.w	filenumber,-(sp)			;fclose()
		move.w	#$3e,-(sp)				;
		trap	#1					;
		addq.l	#4,sp					;
		
		lea.l	icecheck,a0				;check if icepacked
		cmp.l	#"ICE!",(a0)				;
		bne.s	.noice					;
		move.l	8(a0),reservelength			;if ice, use depacked size

.noice:		move.l	reservelength,-(sp)			;malloc()
		move.w	#72,-(sp)				;
		trap	#1					;
		addq.l	#6,sp					;
		tst.l	d0					;enough mem?
		bne.s	.loadit					;yes

		move.l	#text_noram,error_text			;no
		bra.w	exit					;exit

.loadit:	move.l	d0,filebuffer				;store for loader & player

		move.w	#0,-(sp)				;fopen()
		move.l	filename,-(sp)				;
		move.w	#$3d,-(sp)				;
		trap	#1					;
		addq.l	#8,sp					;
		
.ok:		move.w	d0,filenumber				;filenumber
		move.l	filebuffer,-(sp)			;buffer
		move.l	filelength,-(sp)			;length
		move.w	filenumber,-(sp)			;filenumber
		move.w	#$3f,-(sp)				;
		trap	#1					;
		lea	12(sp),sp				;


		move.w	filenumber,-(sp)			;fclose()
		move.w	#$3e,-(sp)				;
		trap	#1					;
		addq.l	#4,sp					;

		move.l	filebuffer,a0				;depack ice
		jsr	ice_decrunch_2				;

		move.l	filebuffer,a0				;dirty SNDH check
		move.l	#text_filetype,error_text		;
		cmp.l	#"SNDH",12(a0)				;
		bne.w	exit					;
		move.l	#new_row,error_text			;